package com.wtwd.campus.utils;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;

/**
 * @Author ldaoliang
 * @Date create in 11:26 2021/2/5
 * @Description
 */
public class BinaryUtil {

    /**
     * 将一个int数字转换为二进制的字符串形式。
     *
     * @param num    需要转换的int类型数据
     * @param digits 要转换的二进制位数，位数不足则在前面补0
     * @return 二进制的字符串形式
     */
    public static String toBinary(int num, int digits) {
        int value = 1 << digits | num;
        String bs = Integer.toBinaryString(value); //0x20 | 这个是为了保证这个string长度是6位数
        return bs.substring(1);
    }

    public static String hexStringCompleteData(String str, int byteNum) {
        if (byteNum == 1) {
            if (str.length() == 1) {
                str = "0" + str;
            }
        } else if (byteNum == 2) {
            if (str.length() == 1) {
                str = "000" + str;
            } else if (str.length() == 2) {
                str = "00" + str;
            } else if (str.length() == 3) {
                str = "0" + str;
            }
        }
        return str.toUpperCase();
    }

    /**
     * String转BCD字节数组
     * @param s
     * @return
     */
    public static byte[] Str2BCDBytes(String s) {
        if (s.length() % 2 != 0) {
            s = "0" + s;
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        char[] cs = s.toCharArray();
        for (int i = 0; i < cs.length; i += 2) {
            int high = cs[i] - 48;
            int low = cs[i + 1] - 48;
            baos.write(high << 4 | low);
        }
        return baos.toByteArray();
    }

    /**
     * 字节数组转16进制
     * @param buf
     * @return
     */
    public static String byte2HexStr(byte buf[]) {
        StringBuffer sb = new StringBuffer(buf.length * 2);
        int i;
        for (i = 0; i < buf.length; i++) {
            if (((int) buf[i] & 0xff) < 0x10)
                sb.append("0");
            sb.append(Long.toString((int) buf[i] & 0xff, 16));
        }
        return sb.toString();
    }

    // 亦或算法
    private static String xor(String strHex_X,String strHex_Y){
        //将x、y转成二进制形式
        String anotherBinary=Integer.toBinaryString(Integer.valueOf(strHex_X,16));
        String thisBinary=Integer.toBinaryString(Integer.valueOf(strHex_Y,16));
        String result = "";
        //判断是否为8位二进制，否则左补零
        if(anotherBinary.length() != 8){
            for (int i = anotherBinary.length(); i <8; i++) {
                anotherBinary = "0"+anotherBinary;
            }
        }
        if(thisBinary.length() != 8){
            for (int i = thisBinary.length(); i <8; i++) {
                thisBinary = "0"+thisBinary;
            }
        }
        //异或运算
        for(int i=0;i<anotherBinary.length();i++){
            //如果相同位置数相同，则补0，否则补1
            if(thisBinary.charAt(i)==anotherBinary.charAt(i))
                result+="0";
            else{
                result+="1";
            }
        }
        return Integer.toHexString(Integer.parseInt(result, 2));
    }

    public static String checkCode(String para) {
        int length = para.length() / 2;
        String[] dateArr = new String[length];

        for (int i = 0; i < length; i++) {
            dateArr[i] = para.substring(i * 2, i * 2 + 2);
        }
        String code = "00";
        for (int i = 0; i < dateArr.length; i++) {
            code = xor(code, dateArr[i]);
        }
        return hexStringCompleteData(code.toUpperCase(),1);
    }

    public static byte[] hexStr2Byte(String secret_key) {
        if (secret_key.length() < 1)
            return null;
        byte[] encrypted = new byte[secret_key.length() / 2];
        for (int i = 0; i < secret_key.length() / 2; i++) {
            int high = Integer.parseInt(secret_key.substring(i * 2, i * 2 + 1), 16);//偶数位
            int low = Integer.parseInt(secret_key.substring(i * 2 + 1, i * 2 + 2), 16);//奇数位
            encrypted[i] = (byte) (high * 16 + low);
        }
        return encrypted;
    }

    public static int hexString2Int(String hexStr) {
        BigInteger bi = new BigInteger(hexStr, 16);
        return bi.intValue();
    }

    public static String int2fourCharHexStr(int valueTen) {
        String strHex = String.format("%06x",valueTen);
        return strHex;
    }




    public static void main(String[] args) throws UnsupportedEncodingException {

        System.out.println(checkCode("000206020159489512350001".trim()));

        String s = "fa0b19f9b5644cc7b4b50605294a4c5a";
        System.out.println(byte2HexStr(s.getBytes("GBK")));
    }
}
